package com.usian.common.threadpool;

import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zlm
 * @version 1.0
 * @date 2022/9/28 18:30
 */
@Data
@Configuration
@EnableAsync//开启异步请求
public class ThreadPoolConfig {
    private static final int corePoolSize = 10;     //核心线程数(默认线程数)
    private static final int maxPoolSize = 100;     //最大线程数
    private static final int keepAliveTime = 10;    //允许线程空闲时间(单位：默认为秒)
    private static final int queueCapacity = 500;   //缓冲队列数

    /**
     * 默认异步线程池
     *
     * @return
     */
    @Bean("searchHistory")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setThreadNamePrefix("----------------全局线程池----------------");
        pool.setCorePoolSize(corePoolSize);
        pool.setMaxPoolSize(maxPoolSize);
        pool.setKeepAliveSeconds(keepAliveTime);
        pool.setQueueCapacity(queueCapacity);
        //pool.setBeanName("");
        /*
         * 拒绝处理策略
         * CallerRunsPolicy()：交由调用方线程运行，比如 main 线程。
         * AbortPolicy()：直接抛出异常。
         * DiscardPolicy()：直接丢弃。
         * DiscardOldestPolicy()：丢弃队列中最老的任务。
         */
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化
        pool.initialize();
        return pool;
    }
}
